package com.webserver.http;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/**
 *  请求对象
 *  该类的每一个实例表示客户端发送过来的一个请求.
 *  每个请求由三部分构成：
 *  1:请求行
 *  2:消息头
 *  3:消息正文
 */
public class HttpServletRequest {
    private Socket socket;
    private String method; //请求方式
    private String uri; //抽象路径
    private String protocol; //版本协议
    //消息头相关信息
    private Map<String,String> headers = new HashMap<>();


    public HttpServletRequest(Socket socket) throws IOException {
        this.socket = socket;
        //1.解析请求行
        parseRequestLine();
        //2.解析消息头
        parseHeader();
        //3.解析消息正文
        parseContent();

    }

    /**
     * 解析请求行
     * @throws IOException
     */
    private void parseRequestLine() throws IOException {
        String line = readLine();
        System.out.println("请求行:"+line);
        String[] data = line.split("\\s");
        method = data[0];
        uri = data[1];
        protocol = data[2];
        System.out.println("method:"+method);
        System.out.println("uri:"+uri);
        System.out.println("protocol:"+protocol);
    }

    /**
     *  解析消息头
     * @throws IOException
     */
    private void parseHeader() throws IOException {
        while (true){
            String line = readLine();
            if (line.isEmpty()){ //如果读取的是空字符串说明单独读取了CRLF
                break;
            }
            System.out.println(line);
            String[] data = line.split(":\\s");
            headers.put(data[0],data[1]);
        }
        System.out.println("headers:"+headers);
    }
    private void parseContent(){

    }

    /**
     * 按行读取客户端发送的消息
     * @return 去除空白后的字符串
     * @throws IOException
     */
    private String readLine() throws IOException {
        InputStream in = socket.getInputStream();
        StringBuilder builder = new StringBuilder();
        char cur = 'a'; //保存本次读取到的字符
        char pre = 'a'; //保存上次读取到的字符
        int d;
        while ((d=in.read()) != -1){
            cur = (char)d; //本次读取到的字符
            if (pre==13 && cur==10){ //判断是否连续读取到了回车+换行
                break;
            }
            builder.append(cur);
            pre = cur; //下次循环前将本次读取到的字符记为上次读取的字符
        }
        return builder.toString().trim();//trim()去除字符串两端空白字符
    }


    public String getMethod() {
        return method;
    }

    public String getUri() {
        return uri;
    }

    public String getProtocol() {
        return protocol;
    }

    public String getHeader(String name){
        return headers.get(name);
    }
}
